package com.example.mulittenantdemo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mulittenantdemo.domain.User;
import com.example.mulittenantdemo.mapper.UserMapper;
import com.example.mulittenantdemo.service.UserService;
import com.example.mulittenantdemo.util.JwtUtil;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.sql.Wrapper;
import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    public String login(String user, String pwd) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user",user).eq("pwd",pwd);
        List<User> users = baseMapper.selectList(wrapper);
        if(CollectionUtil.isEmpty(users)){
            throw new RuntimeException("用户名或者密码不正确");
        }
        if(users.size()>1){
            throw new RuntimeException("用户名或者密码不正确");
        }
        String token = JwtUtil.createToken(users.get(0));
        return token;
    }
}




